package org.dromara.third.convert.fireWork.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.FireWork;
import org.dromara.system.mapper.FireWorkMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.fireWork.dto.FireWorkDTO;
import org.dromara.third.convert.fireWork.validator.FireWorkValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertFireWorkController {

    @Autowired
    private FireWorkMapper fireWorkMapper;

    @PostMapping("/fireWork")
    @Transactional
    public R<Void> addFireWork(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        FireWorkDTO fireWorkDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            fireWorkDTO = JSONObject.parseObject(decrypt, FireWorkDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, fireWorkDTO, new FireWorkValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        FireWork fireWork = assembleFireWorkData(orgCode, fireWorkDTO);
        int count = fireWorkMapper.countWithOutDelFlag(fireWork.getId());
        if (count == 0) {
            fireWorkMapper.insert(fireWork);
        } else {
            fireWorkMapper.updateWithOutDelFlag(fireWork);
        }
        return R.ok();
    }

    private FireWork assembleFireWorkData(String orgCode, FireWorkDTO dto) {
        FireWork fireWork = new FireWork();
        try {
            fireWork.setId(dto.getZyid());
            fireWork.setZyzbh(dto.getZyzbh());
            fireWork.setSqdw(dto.getSqdw());
            fireWork.setZysqsj(DateUtils.parseDate(dto.getZysqsj()));
            fireWork.setSqr(dto.getSqr());
            fireWork.setZynr(dto.getZynr());
            fireWork.setZywz(dto.getZywz());
            fireWork.setDhzyjb(dto.getDhzyjb());
            fireWork.setDhfs(dto.getDhfs());
            fireWork.setZyryxx(dto.getZyryxx());
            fireWork.setZydwlx(dto.getZydwlx());
            fireWork.setZydwmc(dto.getZydwmc());
            fireWork.setZyfzr(dto.getZyfzr());
            fireWork.setZysskssj(DateUtils.parseDate(dto.getZysskssj()));
            fireWork.setZyssjssj(DateUtils.parseDate(dto.getZyssjssj()));
            fireWork.setSfsjqttszy(dto.getSfsjqttszy());
            fireWork.setSjqttszyzbh(dto.getSjqttszyzbh());
            fireWork.setFxbs(dto.getFxbs());
            fireWork.setAqjdr(dto.getAqjdr());
            fireWork.setJsjdr(dto.getJsjdr());
            fireWork.setJhr(dto.getJhr());
            fireWork.setDqzt(dto.getDqzt());
            fireWork.setLongitude(dto.getLongitude());
            fireWork.setLatitude(dto.getLatitude());
            fireWork.setStartTime(DateUtils.parseDate(dto.getStartTime()));
            fireWork.setEndTime(DateUtils.parseDate(dto.getEndTime()));
            fireWork.setSpgbbm(dto.getSpgbbm());
            fireWork.setFhyp(dto.getFhyp());
            fireWork.setAttachment(dto.getAttachment());
            fireWork.setDelFlag(Long.parseLong(dto.getDelFlag()));
            fireWork.setCreateDept(Long.parseLong(dto.getSsbm()));
            fireWork.setCreateBy(Long.parseLong(dto.getCreateBy()));
            fireWork.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            fireWork.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            fireWork.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            fireWork.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装动火作业信息异常");
        }
        return fireWork;
    }
}
